package com.dhu.controller;

import com.alibaba.fastjson.JSONObject;
import com.dhu.pojo.Post;
import com.dhu.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;

@CrossOrigin
@RestController
public class PostController {

    @Autowired
    private PostService postService;

    @GetMapping(value = "/getPostInfo", produces = "application/json;charset=utf-8")
    public String getPostInfo(@RequestParam int id) {
        JSONObject res = new JSONObject();
        Post post = postService.selectPostById(id);
        if (post != null) {
            res.put("code", 200);
            res.put("message", "获取帖子数据成功");
            res.put("data", post);
        } else {
            res.put("code", 404);
            res.put("message", "获取帖子信息失败");
        }
        return res.toJSONString();
    }

    // 获取首页帖子
    @GetMapping(value = "/getHomePostList", produces = "application/json;charset=utf-8")
    public String getHomePostList(int num) {
        JSONObject res = new JSONObject();
        List<Post> posts = postService.selectHomePostList(num);
        if (posts !=null) {
            if (posts.size() > 0) {
                res.put("code", 200);
                res.put("postList", posts);
                res.put("message", "获取首页列表成功");
            } else {
                res.put("code", 404);
                res.put("message", "没有找到帖子");
            }
        } else {
            res.put("code", 403);
            res.put("message", "获取帖子列表失败");
        }
        return res.toJSONString();
    }

    // 获取关注用户的贴子
    @GetMapping("/getFollowPost")
    public String getFollowPost (int id) {
        JSONObject res = new JSONObject();
        List<Post> posts = postService.selectByFollowId(id);
        if (posts != null) {
            res.put("code", 200);
            res.put("postList", posts);
            res.put("message", "获取关注列表成功");
        } else {
            res.put("code", 200);
            res.put("message", "关注的人还没发帖");
        }
        return res.toJSONString();
    }

    // 根据用户id获取帖子
    @GetMapping("/getUserPost")
    public String getUserPost (int userId) {
        JSONObject res = new JSONObject();
        List<Post> posts = postService.selectByUserId(userId);
        if ( posts != null) {
            res.put("code", 200);
            res.put("postList", posts);
            res.put("message", "获取用户帖子成功");
        } else {
            res.put("code", 403);
            res.put("message", "获取用户帖子失败");
        }
        return res.toJSONString();
    }

    // 获取吧帖子
    @GetMapping(value = "/getBarPost", produces = "application/json;charset=utf-8")
    public String getBarPost (@RequestParam int barId) {
        JSONObject res = new JSONObject();
        List<Post> posts = postService.selectPostByBarId(barId);
        if (posts !=null) {
            if (posts.size() > 0) {
                res.put("code", 200);
                res.put("postList", posts);
                res.put("message", "获取吧帖子成功");
            } else {
                res.put("code", 200);
                res.put("message", "还没有帖子");
            }
        } else {
            res.put("code", 403);
            res.put("message", "获取吧帖子失败");
        }
        return res.toJSONString();
    }

    @RequestMapping("/createPost")
    public String createPost (@RequestBody Post post) {
        JSONObject res = new JSONObject();
        if (postService.createPost(post) == 1) {
            res.put("code", 200);
            res.put("message", "发布成功");
        } else {
            res.put("code", 403);
            res.put("message", "发布失败");
        }
        return res.toJSONString();
    }

    @RequestMapping("createImgPost")
    public String createImgPost (Post post,@RequestParam("file") MultipartFile[] files, HttpServletRequest req) throws Exception{
        JSONObject res = new JSONObject();
        StringBuilder media = new StringBuilder();
        String realPath = ResourceUtils.getURL("classpath:").getPath() + "static";
        for (MultipartFile file : files) {
            String name = UUID.randomUUID().toString() + file.getOriginalFilename();
            media.append("#").append(name);
            file.transferTo(new File(realPath, name));
        }
        post.setMedia(media.toString());
        if (postService.createPost(post) == 1) {
            res.put("code", 200);
            res.put("message", "发布成功");
        } else {
            res.put("code", 403);
            res.put("message", "发布失败");
        }
        return res.toJSONString();
    }
}
